package com.ship.dispatch.bean;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ship.common.core.base.PageBean;
import com.ship.common.core.exception.SystemException;
import com.ship.common.core.mapping.Label;
import com.ship.common.core.utils.BeanUtils;
import com.ship.common.core.utils.FieldUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import tk.mybatis.mapper.util.StringUtil;

import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 31463
 */
@Getter
@Setter
public class BaseBean extends PageBean implements Serializable {

    private static final long serialVersionUID = -1104809081550077588L;

    @Id
    @ExcelIgnore
    private Long id;

    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @ExcelIgnore
    private Date createDate;

    @TableField(fill = FieldFill.INSERT)
    @ExcelIgnore
    private String createBy;

    @Label(value = "序号")
    @TableField(exist = false)
    @ExcelIgnore
    private Integer orderNumber;

    /**
     * 导出时使用参数，选中的IDs
     */
    @TableField(exist = false)
    @ApiModelProperty(value = "导出时使用参数，选中的IDs")
    @ExcelIgnore
    private List<String> ids;



    public static String getTableName(Class<?> clazz) {
        Table table = clazz.getAnnotation(Table.class);
        if (table == null) {
            throw new SystemException("类【" + clazz.getName() + "】未配置@Table注解");
        }
        String tableName = table.name();
        if ("".equals(tableName)) {
            return StringUtil.camelhumpToUnderline(clazz.getSimpleName());
        }
        return tableName;
    }

    @Override
    public BaseBean clone() {
        try {
            BaseBean newBean = this.getClass().newInstance();
            List<String> properties = new ArrayList<String>();
            for (Field field : FieldUtil.getAllFields(this.getClass())) {
                properties.add(field.getName());
            }
            BeanUtils.copyPropertiesWithoutNull(this, newBean, properties.toArray(new String[]{}), null);
            return newBean;
        } catch (InstantiationException | IllegalAccessException e) {
        }
        return null;
    }

}
